const jsonServer = require('json-server');//在node里面使用json-server包
const Mock = require('mockjs');
let devApi='/dev-api';//定义路由根别名


//console.log("jsonServer=",jsonServer);
//创建服务器
const server = jsonServer.create();//创建jsonserver 服务对象
//console.log("jsonServer.create()=",server);

//响应
server.use((request, res, next) => {//可选 统一修改请求方式
  console.log("server.use")
  // request.method = 'GET';
  next();
});
//登录注册校验
let mr = Mock.Random;//提取mock的随机对象
server.get(devApi+'/user/login', (req, res) => {
  // console.log(req.query, req.body);//抓取提交过来的query和body
  let username=req.query.username;
  let password=req.query.password;
  (username === 'admin') ?
  res.jsonp({
    "err": 0,
    "msg": "登录成功",
    "data": {
      "follow": mr.integer(1,5),
      "fans": mr.integer(1,5),
      "nikename": mr.cname(),
      "icon": mr.image('20x20',mr.color(),mr.cword(1)),
      "time": mr.integer(13,13)
    }
  }) : (username === 'editor') ?
  res.jsonp({
    "err": 0,
    "msg": "登录成功",
    "data": {
      "follow": mr.integer(1,5),
      "fans": mr.integer(1,5),
      "nikename": mr.cname(),
      "icon": mr.image('20x20',mr.color(),mr.cword(1)),
      "time": mr.integer(13,13)
    }
  }) :
  res.jsonp({
    "err": 1,
    "msg": "登录失败",
  })
});
server.post(devApi+'/user/login', (req, res) => {
  console.log('/user/login=', req.query, req.body);//抓取提交过来的query和body
  let username=req.body.username;
  let password=req.body.password;
  //(username === 'aa' && password === 'aa123')?
  (username === 'admin') ?
    res.jsonp({
      "err": 0,
      "msg": "登录成功",
      "data": {
        "follow": mr.integer(1,5),
        "fans": mr.integer(1,5),
        "nikename": mr.cname(),
        "icon": mr.image('20x20',mr.color(),mr.cword(1)),
        "time": mr.integer(13,13)
      }
    }) : (username === 'editor') ?
    res.jsonp({
      "err": 0,
      "msg": "登录成功",
      "data": {
        "follow": mr.integer(1,5),
        "fans": mr.integer(1,5),
        "nikename": mr.cname(),
        "icon": mr.image('20x20',mr.color(),mr.cword(1)),
        "time": mr.integer(13,13)
      }
    }) :
    res.jsonp({
      "err": 1,
      "msg": "登录失败",
    })
});
server.post(devApi+'/reg', (req, res) => {
  let username=req.body.username;
  (username !== 'aa') ?
    res.jsonp({
      "err": 0,
      "msg": "注册成功",
      "data": {
        "follow": mr.integer(0,0),
        "fans": mr.integer(0,0),
        "nikename": mr.cname(),
        "icon": mr.image('20x20',mr.color(),mr.cword(1)),
        "time": mr.integer(13,13)
      }
    }) :
    res.jsonp({
      "err": 1,
      "msg": "注册失败",
    })

});











module.exports = function (req, res, next) {
    //console.log("req=",req);
    //console.log("res=",res);
    req.method = "GET";
    next();
}

